﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditMedia : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) { Media = o as Media; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(Media); } }
        public string VisualizerName { get { return "EditMedia"; } }

        public static readonly DependencyProperty MediaProperty = DependencyProperty.Register(
            "Media", typeof(Media), typeof(EditMedia), new PropertyMetadata(null, new PropertyChangedCallback(MediaChanged)));

        public Media Media
        {
            get { return (Media)GetValue(MediaProperty); }
            set { SetValue(MediaProperty, value); }
        }

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditMedia, T2> action)
        {
            d.HandleChange<EditMedia, T2>(e, action, (t) => t.UpdateBindings());
        }

        private static void MediaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<Media>(d, e, (t, val) => t.Media = val);
        }

        public override void UpdateBindings()
        {
            if (Media != null)
            {
                gridData.DataContext = Media;
            }
        }

        public override string TitleText
        {
            get
            {
                return (Media == null) ? string.Empty :
                    string.Format("Media {0} ({1} Rooms)", Media.FileName);
            }
        }

        public EditMedia()
        {
            InitializeComponent();
        }

        public override void OnSaveData()
        {
            DataAccess.Client.StoreMediaAsync(DataAccess.AssignedUniqueId, Media);
        }

        private void EditAssociatedIDCollection_RequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnRequestEditID(sender, e);
        }

        private void editGameThing_RequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            OnRequestEditID(sender, e);
        }
    }
}